/*
 *  buttons.c
 *  halo3timer
 *
 *  Created by Phillip LeBlanc on 10/22/08.
 *  Copyright 2008 Team FEV. All rights reserved.
 *
 */


#include "common.h"

#define LINE_SIZE 100
#define LINE_COUNT 20


//*********************Define all of the maps******************// 
map guardian;
map narrows;
map construct;
map pit;
//**********************End Define******************************//

button coords[4];
button gtype_coords[5];

//Initialize all of the maps to their respective settings



//Load map routine
int loadMap(map *themap, char *mapsettings)
{
	FILE *mapfile;
	char lines[LINE_COUNT * LINE_SIZE];
	int i = 0;
	//fprintf(stderr, "Entering loadMap\n");
	
	mapfile = fopen(mapsettings, "r");
	if (!mapfile) {
		fprintf(stderr, "Error when trying to open file: %s", mapsettings);
		return(0);
		}
	
	lines[0] = '\0';  //Prepare the string for concatenation
	
	while ( i < LINE_COUNT) {
		char buffer[LINE_SIZE];
		
		if (fgets ( buffer, sizeof buffer, mapfile) == NULL)
			break;
		
		strcat(lines, buffer);
		i++;
	}
	
	fclose(mapfile);
	
	
	
	//Now "lines" contains all of the information in the file
	//Use strstr and strtok to input values
	
	//fprintf(stderr, "Contents of \"lines\": %s\n", lines);
	
	char* buff = (char *)malloc(sizeof lines);
	strcpy(buff, lines);
	strtok(buff, "\n");
	//themap->mname=*buff;  //Put name into name struct
	
	while ((buff = strtok(NULL, "\n"))!=NULL) {
		i=0;
		//fprintf(stderr, "%s\n", buff);
	
		if (strstr(buff, "HILLMOVE")!=NULL) {
			buff = strstr(buff, "=");
			++buff;
			sscanf(buff, "%d" , &i);
			themap->gtype.hill.hillmove = i;
			//fprintf(stderr, "HILLMOVE sscanf: %d, themap->gtype.hill.hillmove: %d\n", i, themap->gtype.hill.hillmove);
			}
			
		else if (strstr(buff, "SNIPER")!=NULL) {
			buff = strstr(buff, "=");
			++buff;
			sscanf(buff, "%d" , &i);
			themap->sniper = i;
			//fprintf(stderr, "SNIPER sscanf: %d, themap->sniper: %d\n", i, themap->sniper);
			}
			
		else if (strstr(buff, "ROCKETS")!=NULL) {
			buff = strstr(buff, "=");
			++buff;
			sscanf(buff, "%d" , &i);
			themap->rockets = i;
			//fprintf(stderr, "ROCKETS sscanf: %d, themap->rockets: %d\n", i, themap->rockets);
			}
			
		else if (strstr(buff, "OS")!=NULL) {
			buff = strstr(buff, "=");
			++buff;
			sscanf(buff, "%d" , &i);
			themap->over = i;
			//fprintf(stderr, "OS sscanf: %d, themap->over: %d\n", i, themap->over);
			}
			
		else if (strstr(buff, "CAMO")!=NULL) {
			buff = strstr(buff, "=");
			++buff;
			sscanf(buff, "%d" , &i);
			themap->camo = i;
			//fprintf(stderr, "CAMO sscanf: %d, themap->camo: %d\n", i, themap->camo);
			}
		
		else if (strstr(buff, "TIME")!=NULL) {
			buff = strstr(buff, "=");
			++buff;
			sscanf(buff, "%d" , &i);
			themap->gtime = i;
			//fprintf(stderr, "TIME sscanf: %d, themap->gtime: %d\n", i, themap->gtime);
			}
		
		}
	free(buff);
	return 1;
}

int checkCoords(int x, int y, int region)  //Cannot be called before coords is defined
{
	int i;
	
	if (region==MAPS) {
	for (i=0;i<4;i++) {
		if ((x > coords[i].x)&&(x< coords[i].x + coords[i].width)&&(y > coords[i].y)&&(y < coords[i].y + coords[i].height)) {
			return i;
			}  //end if
	   }  //end for
	}	//end if
	else if (region==GTYPE) {
		for (i=0;i<5;i++) {
		if ((x > gtype_coords[i].x)&&(x< gtype_coords[i].x + gtype_coords[i].width)&&(y > gtype_coords[i].y)&&(y < gtype_coords[i].y + gtype_coords[i].height)) {
			return i;
			}  //end if
	   }  //end for
	} //end else if
	
	
	return -1; //what the user clicked was not in the defined region
}
